package team.bailan.common.process.roblord;

import com.alibaba.fastjson.JSON;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import team.bailan.common.enums.CardTypeEnum;
import team.bailan.common.process.dealcard.DealCards;
import team.bailan.common.process.dealcard.InitialCards;
import team.bailan.common.process.ingame.InGame;
import team.bailan.common.process.ingame.InGameMap;
import team.bailan.common.process.ingame.RoomInfMap;
import team.bailan.common.server.WebSocketServer;
import team.bailan.entity.domain.Card;
import team.bailan.entity.domain.CardType;
import team.bailan.entity.domain.State;
import team.bailan.entity.vo.FinalLordInfVO;
import team.bailan.entity.vo.InitialRobberVO;
import team.bailan.entity.vo.RobLordInfVO;

import java.io.IOException;
import java.util.*;


@AllArgsConstructor
@NoArgsConstructor
public class RobLord {

    public int initialPosition;             // 初始是谁来抢地主
    public int nowPosition;                 // 现在轮到谁抢地主
    public int lordPosition = 0;            // 当前地主轮到谁
    public int roomId;                      // 交互的房间号
    public int count = 0;                   // 轮转次数
    public List<Boolean> playerStateList;

    public void init() {
        lordPosition = 0;
        count = 0;
        playerStateList = Arrays.asList(true, true, true, true);
    }

    public void getInitialPosition() throws IOException {
        init();
        Random random = new Random();
        initialPosition = random.nextInt(3) + 1;
        nowPosition = initialPosition;
        InitialRobberVO initialRobberVO = new InitialRobberVO();
        initialRobberVO.setMsgType("InitialPosition");
        initialRobberVO.setPlayerPosition(initialPosition);
        WebSocketServer.webSocketServer.sendToRoom(JSON.toJSONString(initialRobberVO), roomId + "");
    }

    public void robTheLord() throws IOException {
        count += 1;
        lordPosition = nowPosition;
        do {
            nowPosition = nowPosition % 3 + 1;
        } while (!playerStateList.get(nowPosition));
        RobLordInfVO robLordInfVO = new RobLordInfVO();
        robLordInfVO.setMsgType("RobTheLord");
        robLordInfVO.setRobFlag(true);
        robLordInfVO.setNowPosition(lordPosition);
        robLordInfVO.setNextPosition(nowPosition);
        WebSocketServer.webSocketServer.sendToRoom(JSON.toJSONString(robLordInfVO), roomId + "");
        if (RoomInfMap.roomInfMap.containsKey("room_" + roomId)) {
            System.out.println(66666666);
            RoomInfMap.roomInfMap.get("room_" + roomId).getFractionCalculator().redouble();
        }
        if ((count == 3 && getDoNotRobCount() == 2) || count == 4) {
            assignFinalLord();
            if (RoomInfMap.roomInfMap.containsKey("room_" + roomId)) {
                RoomInfMap.roomInfMap.get("room_" + roomId).getFractionCalculator().decrease();
            }
        }
    }

    public void doNotRobTheLord() throws IOException {
        count += 1;
        playerStateList.set(nowPosition, false);
        RobLordInfVO robLordInfVO = new RobLordInfVO();
        robLordInfVO.setMsgType("RobTheLord");
        robLordInfVO.setRobFlag(false);
        robLordInfVO.setNowPosition(nowPosition);
        nowPosition = nowPosition % 3 + 1;
        robLordInfVO.setNextPosition(nowPosition);
        WebSocketServer.webSocketServer.sendToRoom(JSON.toJSONString(robLordInfVO), roomId + "");
        if (count == 3 && getDoNotRobCount() == 3) {
            InGame.init(roomId);
            FinalLordInfVO finalLordInfVO = new FinalLordInfVO();
            finalLordInfVO.setMsgType("Restart");
            finalLordInfVO.setFinalLordPosition(0);
            WebSocketServer.webSocketServer.sendToRoom(JSON.toJSONString(finalLordInfVO), roomId + "");
        } else if (count == 4 || (count == 3 && getDoNotRobCount() == 2)) {
            assignFinalLord();
        }
    }

    public int getDoNotRobCount() {
        int result = 0;
        for (int i = 1; i <= 3; i++) {
            if (!playerStateList.get(i)) {
                result += 1;
            }
        }
        return result;
    }

    public void assignFinalLord() throws IOException {
        FinalLordInfVO finalLordInfVO = new FinalLordInfVO();
        finalLordInfVO.setMsgType("FinalLord");
        finalLordInfVO.setFinalLordPosition(lordPosition);
        WebSocketServer.webSocketServer.sendToRoom(JSON.toJSONString(finalLordInfVO), roomId + "");
        DealCards dealCards = InitialCards.initialCardsMap.get("room_" + roomId);
        dealCards.addLordCardsToPlayer(lordPosition);
        State state = new State(roomId, 0, new CardType(CardTypeEnum.CARD_ARBITRARY, 0, 0), lordPosition);
        Map<String, List<Card>> playerCardsMap = getPlayerCardsMap(dealCards);
        InGame inGame = InGameMap.inGameMap.get("room_" + roomId);
        inGame.setLordPosition(lordPosition);
        inGame.setNowState(state);
        inGame.setPlayerCardsMap(playerCardsMap);
    }

    public Map<String, List<Card>> getPlayerCardsMap(DealCards dealCards) {
        Map<String, List<Card>> playerCardsMap = new HashMap<>();
        playerCardsMap.put("player1", dealCards.getPlayer1Cards());
        playerCardsMap.put("player2", dealCards.getPlayer2Cards());
        playerCardsMap.put("player3", dealCards.getPlayer3Cards());
        return playerCardsMap;
    }
}
